package com.yuntsg.nnsfcp.common.intercepter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuntsg.nnsfcp.front.entity.Employee;
import com.yuntsg.nnsfcp.front.entity.TUserRole;
import com.yuntsg.nnsfcp.front.mapper.EmployeeMapper;
import com.yuntsg.nnsfcp.front.mapper.TUserRoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Component
//登陆拦截
@Slf4j
public class AdminInterceptor implements HandlerInterceptor {

    @Resource
    HttpSession session;

    @Resource
    EmployeeMapper employeeMapper;
    @Resource
    TUserRoleMapper tUserRoleMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        /*
         *后台人员role分为
         * 1 接单员
         * 2 查新员
         * 3 审核员
         * 4 总管理员
         *
         * */
        if (request.getSession().getAttribute("uid") == null) {
//            boolean exist = FileUtil.exist("/home/gcr/isnewdao1/");
            boolean exist = false;
            if (exist) {
                if (session.getAttribute("roleall") == null) {
                    log.info("自己服务器,不需要登录直接略过 默认id 1 单位管理员");
                    Employee employee = employeeMapper.selectById(1);
                    session.setAttribute("user", employee);
//        Role role2 = roleMapper.selectById(employee.getRole());
                    // 下面是role 是旧版本的需要进行转化成新版本权限 废弃
                    List<Integer> roleIds = tUserRoleMapper.selectList(new QueryWrapper<TUserRole>().eq("user_id", employee.getId())).stream().map(o -> o.getRoleId()).collect(Collectors.toList());
//                session.setAttribute("role", 1);
                    session.setAttribute("roleall", roleIds);
                    session.setAttribute("username", employee.getUserName());
                    session.setAttribute("uid", employee.getId());
                    session.setAttribute("type", 3);
                    //当前登录选择的角色id
                    session.setAttribute("roleid", 3);
                    return true;
                } else {
                    return true;
                }

            } else {
                response.setStatus(401);
                return false;
            }

        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
//        System.out.println("postHandle-------请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
//        System.out.println("postHandle-------在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
    }

}